<?php
namespace app\admin\controller;

use \think\facade\Db;
use app\middleware\JWTAUTH;
use \app\admin\model\Keywords;
use \app\Tool\zq\Es;

class Index
{

		public function index(){
			dump(request());exit();
		}


    public function test()
    {
			$data = request()->param();
			if( empty($data['keyworkds']) ){
				return_json( 40000, '关键词不能为空' );
			}else{
				$data['keywords'] = '*美*';
				$server = $_SERVER;
				$kw = new Keywords();
				$res = $kw->addkeys($data['keywords'], $server['REMOTE_ADDR'], $server['SERVER_NAME']);
				if( $res ){
					return_json( 200, '关键词添加成功' );
				}else{
					return_json( 40000, '关键词添加失败' );
				}
			}


		}


		/**
		 *  $name    			搜索关键词
		 *  $starttime   	时间范围 - 开始时间
		 *  $endtime      时间范围 - 结束时间
		 *  $page					分页数
		 *  $limit				每页查询数量
		 *
		 */
		public function search(){
			$page = 1;
			$limit = 5;

			$data = request()->param();
			if( !empty( $data['page'] ) ) $page = $data['page'];
			if( !empty( $data['limit'] ) ) $limit = (int)$data['limit'];
			$from = $limit * ($page-1);
			$search = 1;
			$query = [];

			if( request()->method() == 'GET' ){
				$data['name'] = rawurldecode($data['name']);
			}
			if( !empty( $data['name'] ) ){
				$search = 0;
				// $query['bool']['should'][] = [ 'match' => [ 'name' => '*小*']];
				$query['bool']['should'][] = [
																				[ 'match' => [
																						'name' => [
																							'query' => '*'.$data['name'].'*',
																							'boost' => 3, // 权重大
																						]
																					]
																				]
																			];

			}

			if( !empty( $data['starttime'] ) ){
				$search = 0;

				$query['bool']['filter']['range'] = [
															'time'=>[
																'gte' => (int)$data['starttime'],
															]
														];
			}
			if( !empty( $data['endtime'] ) ){
				$search = 0;

				$query['bool']['filter']['range'] = [
															'time'=>[
																'lte' => (int)$data['endtime'],
															]
														];
			}


			$es = new Es();
			if($search ){
				$re = $es->search_all( $from, $limit );
			}else{
				$re = $es->search_doc( $query, $from, $limit );
			}

			dump($re);

		}
}
